package m.jclecai.com.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.DateUtil;
import m.jclecai.com.utils.MakeRandomUtil;
import m.jclecai.com.view.wheelpicker.WheelPicker;

/**
 * Created by ZD on 2017/4/27.
 * 2,4,7双色球
 * 1,3,6大乐透
 */

public class MyAutoLoopWheel extends LinearLayout {

    //是否停止滚动
    boolean isStop = true;

    @BindView(R.id.wheel1)
    WheelPicker wheel1;
    @BindView(R.id.wheel2)
    WheelPicker wheel2;
    @BindView(R.id.wheel3)
    WheelPicker wheel3;
    @BindView(R.id.wheel4)
    WheelPicker wheel4;
    @BindView(R.id.wheel5)
    WheelPicker wheel5;
    @BindView(R.id.wheel6)
    WheelPicker wheel6;
    @BindView(R.id.wheel7)
    WheelPicker wheel7;
    @BindView(R.id.animation_iv)
    ImageView animation_iv;
    LinkedList<String> redBall;
    LinkedList<String> blueBall;
    int redMax = 35, blueMax = 12;
    int blueCount = 2,redCount = 5;
    int index = 0;

    public MyAutoLoopWheel(Context context) {
        super(context);
        init();
    }

    public MyAutoLoopWheel(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyAutoLoopWheel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init() {
        View view = View.inflate(getContext(), R.layout.view_my_loop, null);
        ButterKnife.bind(this, view);
        addView(view);
        //双色球 红球01-33 蓝球01-16
        //大乐透 红球01-35 蓝球01-12
        if (isSsq()) {
            redCount = 6;
            blueCount = 1;
            redMax = 33;
            blueMax = 16;
        }
        wheel1.setData(getData(redMax));
        wheel1.setAllowClick(false);
        wheel2.setData(getData(redMax));
        wheel2.setAllowClick(false);
        wheel3.setData(getData(redMax));
        wheel3.setAllowClick(false);
        wheel4.setData(getData(redMax));
        wheel4.setAllowClick(false);
        wheel5.setData(getData(redMax));
        wheel5.setAllowClick(false);
        //大乐透两个蓝球
        if (isSsq()) {
            wheel6.setWheel_bg(R.mipmap.ball_red);
            wheel6.setData(getData(redMax));
        } else {
            wheel6.setWheel_bg(R.mipmap.ball_blue);
            wheel6.setData(getData(blueMax));
        }
        wheel6.setAllowClick(false);
        wheel7.setData(getData(blueMax));
        wheel7.setAllowClick(false);

        wheel1.setOnWheelChangeListener(dd);
        wheel2.setOnWheelChangeListener(dd);
        wheel3.setOnWheelChangeListener(dd);
        wheel4.setOnWheelChangeListener(dd);
        wheel5.setOnWheelChangeListener(dd);
        wheel6.setOnWheelChangeListener(dd);
        wheel7.setOnWheelChangeListener(dd);
    }

    WheelPicker.OnWheelChangeListener dd = new WheelPicker.OnWheelChangeListener() {
        @Override
        public void onWheelScrolled(int offset) {
        }

        @Override
        public void onWheelSelected(int position) {
            switch (index){
                case 0:
                    wheel1.setSelectedItemPosition(Integer.valueOf(redBall.get(index))-1,false);
                    break;
                case 1:
                    wheel2.setSelectedItemPosition(Integer.valueOf(redBall.get(index))-1,false);
                    break;
                case 2:
                    wheel3.setSelectedItemPosition(Integer.valueOf(redBall.get(index))-1,false);
                    break;
                case 3:
                    wheel4.setSelectedItemPosition(Integer.valueOf(redBall.get(index))-1,false);
                    break;
                case 4:
                    wheel5.setSelectedItemPosition(Integer.valueOf(redBall.get(index))-1,false);
                    break;
                case 5:
                    String idx;
                    if (!isSsq()) {
                        idx = blueBall.get(0);
                    }else {
                        idx = redBall.get(index);
                    }
                    wheel6.setSelectedItemPosition(Integer.valueOf(idx)-1,false);
                    break;
                case 6:
                    wheel7.setSelectedItemPosition(Integer.valueOf(blueBall.get(blueBall.size()-1))-1,false);
                    //停止滚动
                    isStop = true;
                    Log.e("onWheelSelected", position + "_" + new Gson().toJson(getNumStr())
                            + "\n" + new Gson().toJson(redBall)
                            + "\n" + new Gson().toJson(blueBall));
                    if (callback != null)
                        callback.callbackSuccess();
                    break;
            }
            index++;
        }

        @Override
        public void onWheelScrollStateChanged(int state) {
        }
    };

    public List<String> getData(int maxNum) {
        ArrayList<String> list = new ArrayList();
        for (int i = 1; i < maxNum; i++) {
            if (i < 10)
                list.add("0" + (i));
            else
                list.add("" + (i));
        }
        return list;
    }

    private void startScroll() {
        isStop = false;
        Random random = new Random();
        int rdom = random.nextInt(1000);
        wheel1.startScroll(3000 + rdom);
        wheel2.startScroll(4500 + rdom);
        wheel3.startScroll(6000 + rdom);
        wheel4.startScroll(7500 + rdom);
        wheel5.startScroll(9000 + rdom);
        wheel6.startScroll(10500 + rdom);
        wheel7.startScroll(12000 + rdom);
    }

    /**
     * 获取当前中奖号码
     *
     * @return
     */
    public String[] getNumArr() {
        String[] num = new String[7];
        num[0] = wheel1.getCurrData();
        num[1] = wheel2.getCurrData();
        num[2] = wheel3.getCurrData();
        num[3] = wheel4.getCurrData();
        num[4] = wheel5.getCurrData();
        num[5] = wheel6.getCurrData();
        num[6] = wheel7.getCurrData();
        return num;
    }

    public List<String> getBlueArr() {
        List<String> blue = new LinkedList<>();
        if (isSsq()) {//双色球 1蓝
            blue.add(wheel7.getCurrData());
        } else {
            blue.add(wheel6.getCurrData());
            blue.add(wheel7.getCurrData());
        }
        return blue;
    }

    public List<String> getRedArr() {
        List<String> red = new LinkedList<>();
        red.add(wheel1.getCurrData());
        red.add(wheel2.getCurrData());
        red.add(wheel3.getCurrData());
        red.add(wheel4.getCurrData());
        red.add(wheel5.getCurrData());
        if (isSsq()) {//双色球 6红
            red.add(wheel6.getCurrData());
        }
        return red;
    }

    /**
     * 获取当前中奖号码
     *
     * @return
     */
    public String getNumStr() {
        StringBuilder sb = new StringBuilder();
        sb.append(wheel1.getCurrData());
        sb.append(",");
        sb.append(wheel2.getCurrData());
        sb.append(",");
        sb.append(wheel3.getCurrData());
        sb.append(",");
        sb.append(wheel4.getCurrData());
        sb.append(",");
        sb.append(wheel5.getCurrData());
        if (isSsq()) {
            sb.append(",");
        } else {
            sb.append("|");
        }
        sb.append(wheel6.getCurrData());
        if (isSsq()) {
            sb.append("|");
        } else {
            sb.append(",");
        }
        sb.append(wheel7.getCurrData());
        sb.append(":1:1");
        return sb.toString();
    }

    public String getNumStrNoB() {
        StringBuilder sb = new StringBuilder();
        sb.append(wheel1.getCurrData());
        sb.append(",");
        sb.append(wheel2.getCurrData());
        sb.append(",");
        sb.append(wheel3.getCurrData());
        sb.append(",");
        sb.append(wheel4.getCurrData());
        sb.append(",");
        sb.append(wheel5.getCurrData());
        if (isSsq()) {
            sb.append(",");
        } else {
            sb.append("|");
        }
        sb.append(wheel6.getCurrData());
        if (isSsq()) {
            sb.append("|");
        } else {
            sb.append(",");
        }
        sb.append(wheel7.getCurrData());
        return sb.toString();
    }

    @OnClick(R.id.animation_iv)
    public void start() {
        index = 0;
        redBall =
                MakeRandomUtil.getBall(redMax, redCount) ;
        blueBall =
                MakeRandomUtil.getBall(blueMax, blueCount) ;
        animation_iv.setImageResource(R.drawable.lottery_animlist);
        AnimationDrawable animationDrawable = (AnimationDrawable) animation_iv.getDrawable();
        animationDrawable.setOneShot(true);
        animationDrawable.start();
        int duration = 0;
        for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
            duration += animationDrawable.getDuration(i);
        }
        animation_iv.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isStop)
                    startScroll();
            }
        }, duration);
    }

    public Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    /**
     * 按照周几来判断是什么球种
     * 2,4,7双色球 1蓝
     * 1,3,6大乐透 2蓝
     *
     * @return
     */
    public boolean isSsq() {
        int week = DateUtil.dayOfWeek();
        if (week == 1 || week == 3 || week == 6) {//大乐透
            return false;
        } else {//双色球
            return true;
        }
    }
}
